/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vendingmachine;

import java.util.Random;
import java.util.Scanner;

/**
 *
 * Nama File    : ObjectReceiver.Java
 * Deskripsi    : deskripsi dan implementasi kelas ObjectReceiver yang merupakan penerima Object dalam Vending Machine
 * Nama Pembuat : Muhammad Ghufron Mahfudhi (23512066) dan Jonathan Ery Pradana (23512038)
 * Hari/Tanggal : Senin / 26 November 2012
 */
public class ObjectReceiver {
    //atribut
    private boolean valid;
    private static CoinStorage tempStorage;
    private final int maxTempStorageSize = 10;
    private int storageValue;

    //method
    public ObjectReceiver() {
        this.tempStorage = new CoinStorage(maxTempStorageSize);
        this.storageValue = 0;
        this.valid = false;
    }
    
    public int getStorageValue() {
        return storageValue;
    }
    
    public void resetStorageValue(){
        storageValue = 0;
    }

    public static CoinStorage getTempStorage() {
        return tempStorage;
    }

    public boolean isValid() {
        return valid;
    }

    public void resetValid() {
        this.valid = false;
    }
    
    //menerima objek dan mengecek jenis objek
    //jika koin maka dimasukkan ke storage object receiver, jika slug maka dikeluarkan melalui object dispenser
    public void readInput(){
        Scanner scan = new Scanner(System.in);
        int choice;
        System.out.println("Insert object type to be inserted in to vending machine (1, 2, 3, 4): ");
        System.out.println("1. Nickel");
        System.out.println("2. Dime");
        System.out.println("3. Quarter");
        System.out.println("4. Make a new object: ");
        System.out.print(">>");
        choice = scan.nextInt();
        if(choice > 0 && choice < 5){
            Object O;

            if(choice == 1){
                System.out.println("Nickel is inserted.");
                O = new Coin(CoinValidator.getNickel());
            } else if(choice == 2){
                System.out.println("Dime is inserted.");
                O = new Coin(CoinValidator.getDime());
            } else if(choice == 3){
                System.out.println("Quarter is inserted.");
                O = new Coin(CoinValidator.getQuarter());
            } else{
                Random rand = new Random();
                double size = (double) rand.nextInt(50);
                double weight = (double) rand.nextInt(50);
                double thick = (double) rand.nextInt(50);
                int ser_edges = rand.nextInt(300);
                System.out.println("Make and insert a new object: " + size + ", " + weight + ", " + thick + ", " + ser_edges);
                O = new Object(size, weight, thick, ser_edges);
            }
            
            CoinValidator CV = new CoinValidator(O);
            if(CV.isCoin() && (tempStorage.getNickelTray().size() < maxTempStorageSize) && (tempStorage.getDimeTray().size() < maxTempStorageSize) && (tempStorage.getQuarterTray().size() < maxTempStorageSize)){
                //sekali koin valid ditemukan, pemilihan produk dapat dijalankan
                this.valid = true;

                if(CV.getResultCoin().equals(CoinValidator.getNickel())){
                    tempStorage.getNickelTray().add(CV.getResultCoin());
                } else if(CV.getResultCoin().equals(CoinValidator.getDime())){
                    tempStorage.getDimeTray().add(CV.getResultCoin());
                } else if(CV.getResultCoin().equals(CoinValidator.getQuarter())){
                    tempStorage.getQuarterTray().add(CV.getResultCoin());
                }
                
                this.storageValue += CV.getResultCoin().getValue();
                //System.out.println("Sum of money: " + storageValue + " cents");
                UserDisplayNotification notif = new UserDisplayNotification(UserDisplayNotification.COUNT_MONEY, "inserted", storageValue, false);
            } else if(!CV.isCoin()){
                ObjectDispenser objDispenser = new ObjectDispenser(CV.getResultSlug());
            } else{
                ObjectDispenser objDispenser = new ObjectDispenser(CV.getObj());
            }
        } else{
            System.out.println("Pilihan salah, objek tidak dikenali.");
        }
    }
}
